<?php

namespace Sharks\Core\Persistence\QueryBuilder;

use Sharks\Core\Persistence\Db;

/**
 * 非查询类 SQL 执行
 */
class RawSqlQuery extends AbstractQuery {

    protected $stmt;
    protected $sql;
    protected $identifier;

    public function __construct($sql, $identifier) {
        $this->sql = $sql;
        $this->identifier = $identifier;
    }

    /**
     * 绑定SQL查询参数
     * @param $name 参数名称
     * @param $value 参数值
     */
    public function bind($name, $value) {
        if (is_null($this->stmt)) {
            $this->stmt = Db::connector()->prepare($this->sql);
        }
        $this->stmt->bindValue(":$name", $value);
        return $this;
    }

    /**
     * 获取执行返回值
     */
    public function exec() {
        if (is_null($this->stmt)) {
            $this->stmt = Db::connector()->query($this->sql);
        }
        else {
            $this->stmt->execute();
        }
        $rowCount = $this->stmt->rowCount();
        $this->checkError($this->stmt);

        return $rowCount;
    }
}